﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Threading;
using System.Globalization;
using System.Web.Security;
using Nano.Core;

namespace Nano.App_Config
{
    public partial class PageManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadAuthors();

                loadTemplates();

                htmlEditor1.Content = File.ReadAllText(Server.MapPath("PageTemplates/TextFile1.txt"));

                if (!String.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    var db = new DataContext();
                    
                    var page = db.Pages.First(p => p.PageId == int.Parse(Request.QueryString["id"]));

                    txtPageTitle.Text = page.Title;
                    htmlEditor1.Content = page.PageContent;

                    MultiView1.SetActiveView(View2);
                }
            }
        }

        private void loadTemplates()
        {
            var templates = from t in Directory.GetFiles(Server.MapPath("PageTemplates"), "*.txt")
                            select new { Name = Path.GetFileNameWithoutExtension(t) };

            cmbTemplates.DataSource = templates;
            cmbTemplates.DataTextField = "Name";
            cmbTemplates.DataBind();
        }

        private void loadAuthors()
        {
            cmbAuthors.DataSource = Membership.GetAllUsers();
            cmbAuthors.DataTextField = "UserName";
            cmbAuthors.DataValueField = "ProviderUserKey";
            cmbAuthors.DataBind();
        }

        protected void btnSaveDraft_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["id"]))
            {
                var db = new DataContext();
                
                var page = db.Pages.First(p => p.PageId == int.Parse(Request.QueryString["id"]));

                page.Title = txtPageTitle.Text.Trim();
                page.PageContent = htmlEditor1.Content.Trim();

                db.SaveChanges();
            }


            PageMessage.Text = Resources.Nano.Messages.UpdateSuccessfull;
            Panel1.CssClass = "Success";
        }

        protected void cmbTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
            htmlEditor1.Content = File.ReadAllText(Server.MapPath("PageTemplates/") + cmbTemplates.SelectedItem.Text + ".txt");
        }

        //protected override void InitializeCulture()
        //{
        //    //this.UICulture = "fa-IR";
        //    //this.Culture = "fa-IR";

        //    //Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fa-IR");
        //    //Thread.CurrentThread.CurrentUICulture = new CultureInfo("fa-IR");

        //    base.InitializeCulture();
        //}
    }
}